package com.humi.partner.manager.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.esotericsoftware.minlog.Log;
import com.humi.cloud.common.exception.HumiRuntimeException;
import com.humi.cloud.common.model.ResponseCode;
import com.humi.cloud.common.model.Result;
import com.humi.cloud.message.smscode.provider.SMSCodeParam;
import com.humi.cloud.message.smscode.provider.aliyun.AliyunSMSCodeParam;
import com.humi.cloud.message.smscode.provider.aliyun.AliyunSmsCodeProperties;
import com.humi.cloud.message.smscode.sender.SMSCodeSender;
//gitlab.360humi.com/humicloud/humi-cloud-partner.git
import com.humi.cloud.mybatis.support.model.Page;
import com.humi.partner.common.entity.PartnerApplyRecord;
import com.humi.partner.common.entity.PartnerBase;
import com.humi.partner.common.entity.PartnerType;
import com.humi.partner.common.service.PartnerApplyRecordService;
import com.humi.partner.common.service.PartnerBaseService;
import com.humi.partner.common.service.PartnerTypeService;
import com.humi.partner.front.model.PartnerBasePO;
import com.humi.partner.manager.dao.ManagerPartnerBaseMapper;
import com.humi.partner.manager.entity.PartnerBaseQueryPO;

/**
 * 
 * <pre>
 * <b>Description:</b> 
 * <b>Author:</b> TianJunmin
 * <b>Date:</b> 2019年10月11日
 * </pre>
 */
@Service
public class ManagerPartnerBaseService {

	@Autowired
	ManagerPartnerBaseMapper baseMapper;

	@Autowired
	PartnerApplyRecordService partnerApplyRecordService;

	@Autowired
	PartnerBaseService partnerBaseService;

	@Autowired
	PartnerTypeService partnerTypeService;

	@Autowired
	SMSCodeSender smsCodeSender;

	@Autowired
	AliyunSmsCodeProperties smsPro;
	
	/**
	 * 
	 * <pre>
	 * 获取申请伙伴
	 * </pre>
	 * 
	 * @param query
	 * @return
	 */
	public Result getApplyPartnerList(PartnerBaseQueryPO query) {
		Page<PartnerBasePO, PartnerBaseQueryPO> page = new Page<PartnerBasePO, PartnerBaseQueryPO>(query.getCurrent(),
				query.getSize(), query);
		baseMapper.getPage(page);
		return Result.ok(page);
	}

	/**
	 * 
	 * <pre>
	 * 获取伙伴申请审核记录
	 * </pre>
	 * 
	 * @param partnerId
	 * @return
	 */
	public Result getApplyPartnerRecordList(String partnerId) {
		QueryWrapper<PartnerApplyRecord> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(PartnerApplyRecord::getPartnerId, partnerId);

		return Result.ok(partnerApplyRecordService.list(queryWrapper));
	}

	public Result getPartnerDetails(String partnerId) {
		PartnerBasePO po =baseMapper.getOne(partnerId);
		if (po == null) {
			return Result.fail("未找到记录");
		}
		return Result.ok(po);
	}

//	private String getUserPhoneByUserId(String userId) {
//		try {
//			String[] userIds = { userId };
//			Result result = userCenterFeign.findUserByIds(userIds);
//			if (result != null && result.isSuccess() && result.getData() != null) {
//				List<UserBasicInfoVo> users = JsonUtil.toList(JsonUtil.toJson(result.getData()), UserBasicInfoVo.class);
//				if (users != null && users.size() > 0) {
//					return users.get(0).getMobile();
//				}
//			}
//			return null;
//		} catch (Exception e) {
//			Log.error(e.getMessage());
//			return null;
//		}
//	}

	/**
	 * 
	 * <pre>
	 * 审核加入合作伙伴信息
	 * </pre>
	 * 
	 * @param record
	 * @return
	 */
	@Transactional
	public Result applyPartnerInfo(PartnerApplyRecord record) {
		PartnerBase src = partnerBaseService.getById(record.getPartnerId());
		if (src == null) {
			return Result.fail("申请记录不存在!");
		}
		if (src.getApplyStatus() != 0) {
			return Result.fail("此申请记录不需操作");
		}
		if (record.getApplyStatus() == null || record.getApplyStatus() == 0) {
			return Result.fail("请指定审核状态");
		}

		boolean ret = partnerApplyRecordService.save(record);
		if (ret) {
			src.setApplyStatus(record.getApplyStatus());
			ret = partnerBaseService.updateById(src);
			if (!ret) {
				throw HumiRuntimeException.newInstance(ResponseCode.FAILED, "操作失败[1]");
			}
		} else {
			return Result.fail("操作失败[0]");
		}
		PartnerType pt = partnerTypeService.getById(src.getApplyType());
		if (pt == null) {
			throw HumiRuntimeException.newInstance(ResponseCode.FAILED, "操作失败[2]");
		}

		Map<String, String> map = new HashMap<String, String>();
		map.put("type", pt.getPartnerName());
		map.put("status", record.getApplyStatus() == 1 ? "申请已通过审核" : "申请被驳回");

		String phoneNo = null;
		try {
			phoneNo =src.getUserPhone();
		} catch (Exception e) {

		}
		if (!StringUtils.isEmpty(phoneNo)) {
			SMSCodeParam param = ((AliyunSMSCodeParam) AliyunSMSCodeParam.instance().templetBizKey("TPL_APPLY")
					.phoneNumbers(phoneNo)).templetParams(map);
			try {
				smsCodeSender.send(param);
			} catch (Exception e) {
				Log.error(e.getMessage());
			}
		}
		return Result.ok();
	}
}
